package com.stars.easyms.base.util;

import com.stars.easyms.base.constant.HttpHeaderConstants;
import com.stars.easyms.base.http.EasyMsResponseEntity;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * <p>className: EasyMsUtil</p>
 * <p>description: EasyMs相关工具类</p>
 *
 * @author guoguifang
 * @version 1.6.3
 * @date 2020/9/11 11:13 上午
 */
public final class EasyMsUtil {

    /**
     * 判断是否是easy-ms相关url
     *
     * @param requestPath 请求路径
     * @return 是否是easy-ms相关url
     */
    public static boolean isEasyMsUrl(String requestPath) {
        for (String permitUrl : HttpHeaderConstants.EASY_MS_URL) {
            if (PatternMatcherUtil.doUrlPatternMatch(permitUrl, requestPath, true)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 判断是否是EasyMsResponseEntityType类型或类EasyMsResponseEntityType结构类型
     *
     * @param type 类型
     */
    public static boolean isEasyMsResponseEntityType(Type type) {
        Type rawType = type;
        if (type instanceof ParameterizedType) {
            rawType = ((ParameterizedType) type).getRawType();
        }
        if (EasyMsResponseEntity.class.equals(rawType)) {
            return true;
        }
        try {
            Class<?> rawClass = (Class<?>) rawType;
            rawClass.getDeclaredMethod("setRetCode", String.class);
            rawClass.getDeclaredMethod("setRetMsg", String.class);
            rawClass.getDeclaredMethod("setErrorDesc", String.class);
            rawClass.getDeclaredMethod("setBody", Object.class);
        } catch (NoSuchMethodException e) {
            return false;
        }
        return true;
    }

    private EasyMsUtil() {
    }
}
